import time
import unittest
from autoframe.common.functions import open_browser
from autoframe.page.address_management_page import AddressLocation
from autoframe.page.login_page import LoginPage
from autoframe.page.index_page import IndexPage
from HTMLTestRunner import HTMLTestRunner
import os.path


class AddressManagementTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        # 打开浏览器
        driver = open_browser()
        cls.address = AddressLocation(driver)
        cls.login = LoginPage(driver)
        cls.index = IndexPage(driver)

    def test_address_0(self):  # 登入方法
        self.login.get('http://172.16.140.57/ecshop/user.php?act=address_list')
        self.login.input_username('yang123')
        self.login.input_password('123456')
        self.login.btn_login()

    def test_address_01(self):  # 添加地址
        self.address.add_address()
        # 断言  是否保存成功
        information = self.address.font_text('consignee_0')
        self.assertEqual('李先生', information, msg='添加地址失败')

    def test_address_02(self):  # 修改地址
        self.address.modify_address()
        # 断言 是否能正常修改
        information = self.address.font_text('consignee_0')
        self.assertEqual('杨先生', information, msg='修改地址失败')

    def test_address_03(self):  # 点击删除后  点击取消
        self.address.delete_address_cancel()
        information = self.address.font_text('consignee_0')
        time.sleep(1)
        self.assertEqual('杨先生', information, msg='修改地址失败')

    def test_address_04(self):  # 点击删除后 点击确认
        self.address.delete_address_cancel_determine()

        information = self.address.font_text('consignee_0')
        self.assertEqual('', information, msg='修改地址失败')

    @classmethod
    def tearDownClass(cls) -> None:
        # 关闭浏览器
        cls.address.quit()


if __name__ == '__main__':
    discover = unittest.defaultTestLoader.discover('../case', pattern='A*')
    report = os.path.abspath('../report/')  # 定位文件夹位置
    print(report)
    filename = time.strftime('%Y-%m-%d-%H-%M-%S') + '-添加地址自动化.html'  # 使用time输出当前时间+html文件名
    filepath = os.path.join(report, filename)  # 组合

    with open(filepath, mode='wb') as f:  # 文件操作
        runner = HTMLTestRunner(
            title='EcShop测试报告',
            description='添加地址模块自动化',
            tester='杨',
            stream=f,  # 导出测试报告
            verbosity=2  # 测试报告详细程度
        )
        runner.run(discover)
